//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Compilation;
using System.Xml;
using NopSolutions.NopCommerce.Common.Localization;
using NopSolutions.NopCommerce.Common.Shipping;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.Common.ExportImport
{
    /// <summary>
    /// Import manager
    /// </summary>
    public class ImportManager
    {
        private static Dictionary<string, string> GetResources(string content)
        {
            Dictionary<string, string> resources = new Dictionary<string, string>();

            StringReader _sr = new StringReader(content);
            XmlTextReader _xtr = new XmlTextReader(_sr);
            _xtr.XmlResolver = null;
            _xtr.WhitespaceHandling = WhitespaceHandling.None;
            _xtr.Read();

            if ((_xtr.NodeType == XmlNodeType.Element) && (_xtr.Name == "Language"))
            {
                while (_xtr.Read())
                {
                    if ((_xtr.NodeType == XmlNodeType.Element) && (_xtr.Name == "LocaleResource"))
                    {
                        string resourceName = _xtr["Name"];

                        _xtr.Read();
                        if ((_xtr.NodeType == XmlNodeType.Element) && (_xtr.Name == "Value"))
                        {
                            _xtr.Read();
                            if (_xtr.NodeType == XmlNodeType.Text)
                            {
                                string resourceValue = _xtr.Value;
                                resources.Add(resourceName, resourceValue);
                            }
                        }
                    }
                }
            }

            return resources;
        }

        private static int SaveResourceStrings(int LanguageID, Dictionary<string, string> resources)
        {
            int total = 0;
            foreach (KeyValuePair<string, string> kvp in resources)
            {
                string resourceName = kvp.Key;
                string resourceValue = kvp.Value;
                LocaleStringResourceManager.InsertLocaleStringResource(LanguageID, resourceName, resourceValue);
                total++;
            }

            return total;
        }

        /// <summary>
        /// Import locale resources
        /// </summary>
        /// <param name="LanguageID">Language identifier</param>
        /// <param name="content">Recources in XML format</param>
        /// <returns>Impotred resources count</returns>
        public static int ImportResources(int LanguageID, string content)
        {
            int totalLocalStringRecources = 0;
            Dictionary<string, string> resources = GetResources(content);
            totalLocalStringRecources = SaveResourceStrings(LanguageID, resources);
            return totalLocalStringRecources;
        }
    }
}
